package com.htl.exampro.question.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htl.exampro.base.exception.ExamProException;
import com.htl.exampro.base.model.PageResult;
import com.htl.exampro.base.utils.UserUtil;
import com.htl.exampro.question.mapper.QuestionMapper;
import com.htl.exampro.question.pojo.domain.QuestionDO;
import com.htl.exampro.question.pojo.query.QuestionPageParam;
import com.htl.exampro.question.pojo.vo.QuestionVO;
import com.htl.exampro.question.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 试题表 服务实现类
 * </p>
 *
 * @author hehehe
 * @since 2025-05-25
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, QuestionDO> implements QuestionService {

    @Override
    public QuestionVO queryQuestionById(String questionId) {
        QuestionDO questionDO = baseMapper.selectById(questionId);
        return BeanUtil.toBean(questionDO, QuestionVO.class);
    }

    @Override
    public PageResult<QuestionVO> queryQuestionPage(QuestionPageParam pageParam) {
        Page<QuestionDO> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        LambdaQueryWrapper<QuestionDO> queryWrapper = Wrappers.lambdaQuery(QuestionDO.class);
        queryWrapper.eq(QuestionDO::getCreatedPerson, UserUtil.getUserId());
        queryWrapper.eq(QuestionDO::getIsCurrent, true);
        queryWrapper.eq(pageParam.getTypeId() != null, QuestionDO::getTypeId, pageParam.getTypeId());
        queryWrapper.eq(pageParam.getDifficulty() != null, QuestionDO::getDifficulty, pageParam.getDifficulty());
        queryWrapper.like(StringUtils.isNotBlank(pageParam.getContent()), QuestionDO::getContent,
                pageParam.getContent());
        queryWrapper.orderByDesc(QuestionDO::getCreatedTime);
        baseMapper.selectPage(page, queryWrapper);
        return PageResult.result(page, QuestionVO.class);
    }

    @Override
    public int saveQuestion(QuestionVO questionVO) {
        QuestionDO questionDO = BeanUtil.toBean(questionVO, QuestionDO.class);
        return this.baseMapper.insert(questionDO);
    }

    @Override
    @Transactional
    public int updateQuestion(QuestionVO questionVO) {
        QuestionDO questionDO = baseMapper.selectById(questionVO.getQuestionId());
        if (questionDO == null)
            ExamProException.cast("修改失败：题目不存在！");
        if (!questionDO.getIsCurrent())
            ExamProException.cast("修改失败：历史版本不允许修改！（请更新题目为最新版本后方可修改）");
        if (compareQuestion(questionVO, questionDO))
            ExamProException.cast("提交失败：请修改题目后提交保存");

        baseMapper.update(Wrappers.<QuestionDO>lambdaUpdate()
                .set(QuestionDO::getIsCurrent, false)
                .eq(QuestionDO::getQuestionId, questionVO.getQuestionId()));
        QuestionDO qDO = BeanUtil.toBean(questionVO, QuestionDO.class);
        qDO.setQuestionId(null);
        qDO.setYQuestionId(questionDO.getYQuestionId());
        qDO.setVersion(questionDO.getVersion() + 1);
        qDO.setIsCurrent(true);
        return baseMapper.insert(qDO);

    }

    /**
     * 比较要修改的问题是否和之前一样
     */
    private boolean compareQuestion(QuestionVO questionVO, QuestionDO questionDO) {
        if (!questionDO.getTypeId().equals(questionVO.getTypeId()))
            return false;
        if (!questionDO.getContent().equals(questionVO.getContent()))
            return false;
        if (!questionDO.getOptions().equals(questionVO.getOptions()))
            return false;
        if (!questionDO.getAnswer().equals(questionVO.getAnswer()))
            return false;
        if (!questionDO.getAnalysis().equals(questionVO.getAnalysis()))
            return false;
        if (!questionDO.getDifficulty().equals(questionVO.getDifficulty()))
            return false;
        return true;
    }

}
